<script setup lang="ts">
import { ref, reactive, onMounted } from "vue";
import { queryAttractions, getHotRank } from "@/api/tourism";
import { useRouter } from "vue-router";

interface Attraction {
  id: number;
  name: string;
  description: string;
  supplierId: number;
  price: number;
  type: number;
  rating: number;
  createdTime: string;
  updateTime: string;
  avatar: string | null;
}

// 查询参数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 8,
  type: undefined as number | undefined,
});

const attractions = ref<Attraction[]>([]);
const loading = ref(false);
const filterType = ref("all");
const sortType = ref("default");
const hotAttractions = ref<Attraction[]>([]);
const router = useRouter();

// 获取景点列表
const fetchAttractions = async () => {
  loading.value = true;
  try {
    const params: any = {
      pageNo: queryParams.pageNo - 1,
      pageSize: queryParams.pageSize,
    };

    // 只有选择具体类型时才添加type参数
    if (queryParams.type) {
      params.type = queryParams.type;
    }

    // 添加排序参数
    if (sortType.value === "price") {
      params.isPrice = "true";
    } else if (sortType.value === "score") {
      params.isRating = "true";
    }

    const res = await queryAttractions(params);
    if (res.success) {
      attractions.value = res.data;
    }
  } catch (error) {
    console.error("获取景点列表失败：", error);
  } finally {
    loading.value = false;
  }
};

// 获取热门景点排行
const fetchHotRank = async () => {
  try {
    const res = await getHotRank();
    if (res.success) {
      hotAttractions.value = res.data;
    }
  } catch (error) {
    console.error("获取热门景点排行失败：", error);
  }
};

// 获取景点类型标签
const getTypeTag = (type: number) => {
  switch (type) {
    case 1:
      return "自然风光";
    case 2:
      return "人文景观";
    case 3:
      return "农事体验";
    default:
      return "其他";
  }
};

// 切换类型
const handleTypeChange = (type: string) => {
  filterType.value = type;
  switch (type) {
    case "natural":
      queryParams.type = 1;
      break;
    case "cultural":
      queryParams.type = 2;
      break;
    case "activity":
      queryParams.type = 3;
      break;
    default:
      queryParams.type = undefined;
  }
  queryParams.pageNo = 1; // 重置页码
  fetchAttractions();
};

// 切换排序方式
const handleSortChange = (type: string) => {
  sortType.value = type;
  queryParams.pageNo = 1; // 切换排序时重置页码
  fetchAttractions();
};

// 页码改变
const handlePageChange = (page: number) => {
  queryParams.pageNo = page;
  fetchAttractions();
};

// 点击景点跳转到详情页
const handleClick = (id: number) => {
  router.push(`/tourist/attraction/${id}`);
};

onMounted(() => {
  fetchAttractions();
  fetchHotRank();
});
</script>

<template>
  <div class="tourism-container">
    <!-- 左侧：热门景点排行 -->
    <div class="left-section">
      <div class="rank-header">
        <h3>热门景点排行</h3>
      </div>
      <div class="rank-list">
        <el-card
          v-for="(item, index) in hotAttractions"
          :key="item.id"
          class="rank-item"
        >
          <div class="rank-number" :class="`rank-${index + 1}`">
            {{ index + 1 }}
          </div>
          <div class="rank-image-wrapper">
            <el-image :src="item.avatar" class="rank-image">
              <template #error>
                <div class="image-slot">
                  <el-icon><Picture /></el-icon>
                </div>
              </template>
            </el-image>
          </div>
          <div class="rank-content">
            <h4 class="rank-name">{{ item.name }}</h4>
            <p class="rank-desc">{{ item.description }}</p>
            <div class="rank-meta">
              <el-tag
                size="small"
                :type="
                  item.type === 1
                    ? 'success'
                    : item.type === 2
                    ? 'warning'
                    : 'info'
                "
              >
                {{ getTypeTag(item.type) }}
              </el-tag>
              <div class="rank-info">
                <span class="rank-price">{{ item.price }}</span>
                <span class="rank-rating">评分: {{ item.rating }}</span>
              </div>
            </div>
          </div>
        </el-card>
      </div>
    </div>

    <!-- 右侧：景点展示区域 -->
    <div class="right-section">
      <!-- 筛选和排序区域 -->
      <div class="filter-section">
        <div class="type-filter">
          <el-radio-group v-model="filterType" @change="handleTypeChange">
            <el-radio-button label="all">全部景点</el-radio-button>
            <el-radio-button label="natural">自然风光</el-radio-button>
            <el-radio-button label="cultural">人文景观</el-radio-button>
            <el-radio-button label="activity">农事体验</el-radio-button>
          </el-radio-group>
        </div>
        <div class="sort-filter">
          <el-radio-group v-model="sortType" @change="handleSortChange">
            <el-radio-button label="default">默认</el-radio-button>
            <el-radio-button label="price">价格优先</el-radio-button>
            <el-radio-button label="score">评分优先</el-radio-button>
          </el-radio-group>
        </div>
      </div>

      <!-- 景点列表 -->
      <div class="tourism-list" v-loading="loading">
        <el-empty v-if="attractions.length === 0" description="暂无景点" />
        <div class="attractions-grid" v-else>
          <div
            v-for="item in attractions"
            :key="item.id"
            class="attraction-card"
            @click="handleClick(item.id)"
          >
            <el-card :body-style="{ padding: '0px' }">
              <el-image :src="item.avatar" class="tourism-image">
                <template #error>
                  <div class="image-slot">
                    <el-icon><Picture /></el-icon>
                  </div>
                </template>
              </el-image>
              <div class="tourism-info">
                <h3 class="tourism-name">{{ item.name }}</h3>
                <p class="tourism-desc">{{ item.description }}</p>
                <div class="tourism-meta">
                  <span class="price">{{ item.price }}</span>
                  <span class="rating">评分: {{ item.rating }}</span>
                </div>
              </div>
            </el-card>
          </div>
        </div>
      </div>

      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          v-model:current-page="queryParams.pageNo"
          :page-size="queryParams.pageSize"
          :total="100"
          layout="prev, pager, next"
          @current-change="handlePageChange"
        />
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.tourism-container {
  padding: 24px;
  display: grid;
  grid-template-columns: 320px 1fr;
  gap: 24px;
  background: #f8fafc;
  min-height: calc(100vh - 60px);
  max-width: 1440px;
  margin: 0 auto;
}

.left-section {
  background: #fff;
  border-radius: 16px;
  padding: 20px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.06);
  position: sticky;
  top: 24px;
  height: fit-content;
}

.rank-header {
  margin-bottom: 20px;
  padding-bottom: 16px;
  border-bottom: 1px solid #f0f0f0;
}

.rank-header h3 {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
  color: #1a1a1a;
  display: flex;
  align-items: center;

  &::before {
    content: "";
    width: 4px;
    height: 20px;
    background: #409eff;
    margin-right: 8px;
    border-radius: 2px;
  }
}

.right-section {
  background: transparent;
  border-radius: 16px;
  padding: 0;
}

.filter-section {
  margin-bottom: 24px;
  padding: 16px 24px;
  border-bottom: none;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.06);

  .type-filter,
  .sort-filter {
    .el-radio-group {
      margin-right: 0;
      display: flex;
      gap: 8px;
    }

    :deep(.el-radio-button__inner) {
      border-radius: 8px;
      padding: 10px 20px;
      font-size: 14px;
      font-weight: 500;
      transition: all 0.3s ease;
    }

    :deep(.el-radio-button:not(:first-child) .el-radio-button__inner) {
      border-left: 1px solid #dcdfe6;
    }

    :deep(.el-radio-button__orig-radio:checked + .el-radio-button__inner) {
      background: #409eff;
      box-shadow: 0 4px 12px rgba(64, 158, 255, 0.2);
      transform: translateY(-1px);
    }
  }
}

.rank-item {
  position: relative;
  margin-bottom: 20px;
  transition: all 0.3s;
  border-radius: 12px;
  overflow: hidden;

  &:hover {
    transform: translateY(-4px);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);

    .rank-image {
      transform: scale(1.05);
    }
  }

  &:last-child {
    margin-bottom: 0;
  }
}

.rank-number {
  position: absolute;
  top: 12px;
  left: 12px;
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  font-weight: 600;
  color: #fff;
  z-index: 2;
  font-size: 15px;
}

.rank-1 {
  background: linear-gradient(135deg, #ff6b6b, #ff4757);
}

.rank-2 {
  background: linear-gradient(135deg, #ff9f43, #ff7f50);
}

.rank-3 {
  background: linear-gradient(135deg, #ffd93d, #ffb142);
}

.rank-content {
  padding: 16px;
  margin-top: 0;
}

.rank-name {
  margin: 0 0 8px 0;
  font-size: 16px;
  font-weight: 500;
  color: #1a1a1a;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.rank-desc {
  margin: 0 0 12px 0;
  font-size: 14px;
  color: #666;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  line-height: 1.4;
}

.rank-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.rank-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.rank-price {
  color: #ff4d4f;
  font-size: 18px;
  font-weight: 600;
  position: relative;
  padding-left: 12px;

  &::before {
    content: "¥";
    font-size: 14px;
    position: absolute;
    left: 0;
  }
}

.rank-rating {
  font-size: 13px;
  color: #999;
  background: #f5f7fa;
  padding: 4px 12px;
  border-radius: 12px;
}

.tourism-list {
  margin: 0;
  width: 100%;
}

.attractions-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 24px;
  width: 100%;
}

.attraction-card {
  width: 100%;
  transition: all 0.3s;
  border-radius: 16px;
  overflow: hidden;
  height: 100%;

  .el-card {
    height: 100%;
    border: none;
    background: #fff;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.06) !important;
    transition: all 0.3s;

    &:hover {
      transform: translateY(-6px);
      box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12) !important;

      .tourism-image {
        transform: scale(1.05);
      }
    }
  }
}

.tourism-image {
  width: 100%;
  height: 200px;
  object-fit: cover;
  display: block;
  transition: transform 0.3s ease;
}

.tourism-info {
  padding: 16px;
  background: #fff;
}

.tourism-name {
  margin: 0;
  font-size: 16px;
  font-weight: 500;
  color: #1a1a1a;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tourism-desc {
  margin: 8px 0 12px;
  font-size: 14px;
  color: #666;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  line-height: 1.4;
  min-height: 40px;
}

.tourism-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid #f0f0f0;
}

.price {
  color: #ff4d4f;
  font-size: 20px;
  font-weight: 600;
  position: relative;
  padding-left: 14px;

  &::before {
    content: "¥";
    font-size: 14px;
    position: absolute;
    left: 0;
  }
}

.rating {
  color: #999;
  font-size: 13px;
  background: #f5f7fa;
  padding: 4px 12px;
  border-radius: 12px;
}

.pagination {
  margin-top: 32px;
  text-align: center;

  :deep(.el-pagination) {
    justify-content: center;
  }

  :deep(.el-pagination.is-background .el-pager li:not(.is-disabled).is-active) {
    background-color: #409eff;
    box-shadow: 0 4px 12px rgba(64, 158, 255, 0.2);
  }
}

.image-slot {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background: #f5f7fa;
  color: #909399;
}

@media screen and (max-width: 1400px) {
  .tourism-container {
    grid-template-columns: 300px 1fr;
  }

  .attractions-grid {
    grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  }
}

@media screen and (max-width: 992px) {
  .tourism-container {
    grid-template-columns: 1fr;
  }

  .left-section {
    position: static;
  }

  .attractions-grid {
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  }
}

@media screen and (max-width: 768px) {
  .tourism-container {
    padding: 16px;
  }

  .filter-section {
    flex-direction: column;
    gap: 16px;
    padding: 16px;
  }

  .attractions-grid {
    grid-template-columns: 1fr;
    gap: 16px;
  }

  .tourism-image {
    height: 180px;
  }
}
</style> 